package com.yumijinfu.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.adapter.RenChouAdapter;
import com.yumijinfu.base.BaseFragment;
import com.yumijinfu.entity.Page;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.mian.MainActivity;
import com.yumijinfu.pullrefreshlistview.XListView;
import com.yumijinfu.response.RenChouRes;
import com.yumijinfu.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 认筹
 */
public class ConfessFragment extends BaseFragment implements XListView.IXListViewListener {
    @Bind(R.id.titlebar)
    TitleBar titleBar;
    @Bind(R.id.listview)
    XListView mlistview;
    //
    private MainActivity activity;
    private View view;

    private int pageNum = 1;
    private int numPerPage = 10;
    private boolean flag;
    private RenChouAdapter mRenChouAdapter;
    private List<Page> pageinfo = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);
        activity = (MainActivity) getActivity();
        super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {
        titleBar.setTitleBar(0, "认筹", 0);
    }

    @Override
    protected void initView() {

        //
        mlistview.setPullLoadEnable(true);
        mlistview.setXListViewListener(this);
        //
        mRenChouAdapter = new RenChouAdapter(getActivity(), pageinfo);
        mlistview.setAdapter(mRenChouAdapter);


/**
 *
 * listview 事件监听
 */


        mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//           Info info= (Info) adpater.getItem(position);
//                Toast.makeText(getActivity(), info.getName(), Toast.LENGTH_SHORT).show();
//
//                Intent intent=new Intent(getActivity(),RenChouDetailsActivity.class);
//                intent.putExtra("info",info);
//                startActivity(intent);

            }
        });

    }

    @Override
    protected void setData() {

        getdata(pageNum, numPerPage);


    }


    @Override
    protected void widgetClick(View view) {

    }

    /**
     * 获取认筹列表数据
     */
    private void getdata(int pageNum, int numPerPage) {
        HttpParams params = new HttpParams();
        params.put("borrowTypeCode", "RCB");
        params.put("pageNum", pageNum + "");
        params.put("numPerPage", numPerPage + "");

        HttpUtils.post(Config.BORROW_LIST, params, new HttpCallBack(getActivity(), "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (null != result) {
                    try {
                        RenChouRes res = new RenChouRes(new JSONObject(result.toString()));
                        if(flag==true){
                            pageinfo = res.getPage();
                        }else{
                            if(!res.getCurrentPage().equals(res.getTotalPage())){
                                pageinfo.addAll(res.getPage());
                            }else{
                                if(res.getPage().size()>0){
                                    pageinfo.addAll(res.getPage());
                                }else {

                                    Toast.makeText(getActivity(), "无更多加载数据", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                        //
                        mRenChouAdapter.UpdateAdapter(pageinfo);
                        //释放
                        onLoad();


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }


            }

            @Override
            public void onError(String code, String message) {
                onLoad();
                Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onRefresh() {
        flag = true;
        pageNum = 0;
        getdata(pageNum, numPerPage);
    }

    @Override
    public void onLoadMore() {
        flag = false;
        pageNum++;
        getdata(pageNum, numPerPage);


    }

    private void onLoad() {
        mlistview.stopRefresh();
        mlistview.stopLoadMore();
        mlistview.setRefreshTime("刚刚");
    }
}
